﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using fsw.Common;
using fsw.BLL;
using System.Text;

namespace fsw.Web.Data
{
    /// <summary>
    /// GetCard 的摘要说明
    /// </summary>
    [WebService(Description = "GetCard", Name = "GetCard", Namespace = "")]
    [System.ComponentModel.ToolboxItem(false)]
    [WebServiceBinding(ConformsTo = WsiProfiles.None)]
    // 若要允许使用 ASP.NET AJAX 从脚本中调用此 Web 服务，请取消注释以下行。 
    // [System.Web.Script.Services.ScriptService]
    public class GetCard : System.Web.Services.WebService
    {

        [WebMethod]
        public List<string> GetListCard()
        {
            string _Path = HttpContext.Current.Request.ServerVariables["APPL_PHYSICAL_PATH"];
            string _Path_file = DTKeys.Path_File;
            return GetListCard("", _Path, _Path_file);

        }
        #region ##分类##
        /// <summary>
        /// 树型列表
        /// </summary>
        /// <param name="_Url"></param>
        /// <returns></returns>
        private List<string> GetListCard(string _Url, string _Path, string _Path_file)
        {
            List<string> _List = new List<string>();
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(_Path_file + _Url)))
            {
                string[] _dirfile = null;
                if (!string.IsNullOrEmpty(_Url))
                    _dirfile = DirFileHelper.GetDirectories(Server.MapPath(_Path_file + _Url));
                else
                    _dirfile = GetCardList(_Path, _Path_file).ToArray();
                for (int i = 0; i < _dirfile.Length; i++)
                {
                    string _DirFileUrl = _dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                    string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                    int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                    string _DirFileProject = new BLL.Project().GetTitle(_DirFilePid);
                    string _DirFileUrl2 = _DirFileUrl.Replace(_Path_file.Substring(1), "");
                    string _DirFileName = DirFileHelper.GetFileLastName(_dirfile[i]);
                    if (string.IsNullOrEmpty(_Url)) _DirFileName = _DirFileProject;
                    StringBuilder sb = new StringBuilder();
                    sb.Append("{");
                    sb.AppendFormat("id: '{0}', pId: '{1}', name: '{2}', url: 'List.aspx?url={3}', target: 'content_body'", _DirFileUrl2, _Url, _DirFileName, _DirFileUrl2);
                    sb.Append("}");
                    _List.Add(sb.ToString());
                    _List.AddRange(GetListCard(_DirFileUrl2, _Path, _Path_file));
                }
            }
            return _List;
        }
        private List<string> GetCardList(string _Path, string _Path_file)
        {
            List<string> _List = new List<string>();
            Dictionary<int, string> _ListInt = new Dictionary<int, string>();
            if (!DirFileHelper.IsEmptyDirectory(Server.MapPath(_Path_file)))
            {
                string[] _dirfile = DirFileHelper.GetDirectories(Server.MapPath(_Path_file));
                for (int i = 0; i < _dirfile.Length; i++)
                {
                    string _DirFileUrl = _dirfile[i].Replace(_Path, "/").Replace(@"\", "/");
                    string[] _DirFileUrlArr = _DirFileUrl.Split('_');
                    int _DirFilePid = Utils.StringToInt(DirFileHelper.GetFileProject(_DirFileUrlArr[0]));
                    int _OrderNo = GetPidOrderNo(_DirFilePid);
                    _ListInt.Add(_OrderNo, _dirfile[i]);
                }
                Dictionary<int, string> dic1desc = _ListInt.OrderByDescending(o => o.Key).ToDictionary(o => o.Key, p => p.Value);
                foreach (KeyValuePair<int, string> k in dic1desc)
                {
                    _List.Add(k.Value);
                }
            }
            return _List;
        }
        /// <summary>
        /// 
        /// </summary>
        /// <param name="Id"></param>
        /// <returns></returns>
        private int GetPidOrderNo(int Id)
        {
            List<Model.Project> _ListPid = new BLL.Project().GetList();
            foreach (Model.Project m in _ListPid)
            {
                if (m.Pid == Id) return m.OrderNo;
            }
            return 1000;
        }
        #endregion
        #region ###图片路径###
        #endregion
    }
}
